﻿#region File Description
//  DomoBCI 
//  License: GNU/GPL
//  Developer: Federico Leonardo Motta
//  File description: GUI
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace DomoBCI.gui
{   
    class UIbutton : Item
    {
        Texture2D pressSkin;
        Texture2D currentSkin;
        Texture2D activeSkin;
        Texture2D inactiveSkin;
        UIlabel text;
        float scale;
        bool oldstate = false;
        bool oldpress = false;
        bool oldactive = false;
        Vector2 margin= Vector2.Zero;
        static int i = 0;

        public UIbutton(string f, Game Game)
            : base(Game)
        {
            ID = i;
            Name = "Button" + ID;
            i++;
        }

        public UIbutton(String n, Vector2 p, string s, string s1, string s2, string s3, Game Game):
            base(Game,n,p)
        {
            ID = i;
            Name = n;
            Skin = Game.Content.Load<Texture2D>(@s);
            pressSkin = Game.Content.Load<Texture2D>(@s1);
            activeSkin = Game.Content.Load<Texture2D>(@s2);
            inactiveSkin = Game.Content.Load<Texture2D>(@s3);
            currentSkin = Skin;
            i++;
        }

        public UIbutton(String n, Vector2 p, String t,String f, string s, string s1, string s2, string s3, Game Game) :
            base(Game)
        {
            ID = i;
            Name = n;
            Position = p;
            Skin = Game.Content.Load<Texture2D>(@s);
            pressSkin = Game.Content.Load<Texture2D>(@s1);
            activeSkin = Game.Content.Load<Texture2D>(@s2);
            inactiveSkin = Game.Content.Load<Texture2D>(@s3);
            currentSkin = Skin;
            text = new UIlabel(n, t, f, 1, Game);
            margin.X = (Skin.Width - text.Dimension.X)/2;
            margin.Y = (Skin.Height - text.Dimension.Y)/2;
            Dimension = new Vector2(text.Dimension.X * (1 + margin.X), text.Dimension.Y * (1 + margin.Y));
            scale = Skin.Width / text.Dimension.X;
            i++;
        }

        public Texture2D CurrentSkin {
            get { return currentSkin; }
            set { currentSkin = value; }
        }

        public void UpdateButton(GameTime gameTime)
        {
            if (!IsActive)
            {
                currentSkin = inactiveSkin;
                oldactive = true;
            }
            else if(oldactive)
            {
                currentSkin = Skin;
                oldactive = false;
            }

            if (!oldstate && Select)
            {
                currentSkin = activeSkin;
                oldstate = true;
            }else if (oldstate && !Select)
            {
                currentSkin = Skin;
                oldstate = false;
            }

            if (!oldpress && IsPressed) {
                currentSkin = pressSkin;
                oldpress = true;
            }else if(oldpress && !IsPressed){
                currentSkin = activeSkin;
                oldpress = false;
            }
        }

        protected override void Dispose(bool disposing)
        {
            Skin.Dispose();
            activeSkin.Dispose();
            currentSkin.Dispose();
            activeSkin.Dispose();
            Game.Services.RemoveService(typeof(UIbutton));
            base.Dispose(disposing);
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch, float screenscale)
        {
            spriteBatch.Draw(currentSkin, Position, null, Color.White, 0f, Vector2.Zero, screenscale, SpriteEffects.None, 0f);
            Color col = (this.IsActive) ? Color.White : Color.DarkGray;
            spriteBatch.DrawString(text.Font, text.Text, new Vector2(Position.X + (margin.X)*screenscale, Position.Y + (margin.Y)*screenscale), col, 0.0f, new Vector2(0, 0), screenscale , SpriteEffects.None, 0);
        }

    }
}
